package com.apchernykh.rozhlasapp.services;

import com.apchernykh.rozhlasapp.player.IPlayerWrapper;
import com.apchernykh.rozhlasapp.player.MediaPlayerWrapper;
import com.apchernykh.rozhlasapp.player.NullPlayerWrapper;
import com.apchernykh.rozhlasapp.ui.RozhlasAppBaseFragmentActivity;
import com.apchernykh.types.DetailedUrl;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

public final class PlaybackAsyncControlHandler extends Handler {

	public final static int REFRESH_VIEWS = 1;
	public final static int START_PLAYBACK = 2;
	public final static int STOP_PLAYBACK = 3;
	public final static int ERROR_DETECTED = 4;
	public final static int PAUSE_PLAYBACK = 5;
	IPlayerWrapper mPlayerWrapper = new NullPlayerWrapper();
	BackgroundPhoneInetNotifiedService mParent;

	public PlaybackAsyncControlHandler(Looper iLooper,
			BackgroundPhoneInetNotifiedService iBackgroundPlaybackService) {
		super(iLooper);
		mPlayerWrapper = new MediaPlayerWrapper(iBackgroundPlaybackService);
		mParent = iBackgroundPlaybackService;
		// mPlayerWrapper = new
		// TestPlayerWrapper(backgroundPlaybackService);
	}

	@Override
	public void handleMessage(Message iMessage) {
		switch (iMessage.what) {
		case REFRESH_VIEWS: {
			RozhlasAppBaseFragmentActivity.mApplicationModel.mOnViewsExpiredListener
					.onViewExpired();
		}
			break;
		case START_PLAYBACK: {
			DetailedUrl aUrl = (DetailedUrl) iMessage.obj;
			mPlayerWrapper.play(aUrl, 0);
		}
			break;
		case STOP_PLAYBACK: {
			mPlayerWrapper.stop();
		}
			break;
		case PAUSE_PLAYBACK: {
			mPlayerWrapper.pause();
		}
			break;
		case ERROR_DETECTED: {

		}
			break;
		default:
			break;
		}
	}
}